import datetime
import sxtwl
from .ming_pan import MingPan
from .consts import common


def bazi_by_date(year, month, day, hour, minute, gender, date_type='Solar'):
    """
    出生日期：年/月/日/时/日历类型
    Lunar：农历
    Solar：公历
    默认位置北京
    """
    lunar = sxtwl.Lunar()
    if date_type.lower() != 'lunar':
        birthday = datetime.datetime(year, month, day, hour, minute)
    else:  # 通过农历计算公历
        lunar_day = lunar.getDayByLunar(year, month, day)
        birthday = datetime.datetime(lunar_day.y, lunar_day.m, lunar_day.d, hour, minute)
    if birthday.hour >= 23:  # 修正子时
        new_day = birthday + datetime.timedelta(hours=1)
        hour = new_day.hour
        lunar_day = lunar.getDayBySolar(new_day.year, new_day.month, new_day.day)
    else:
        hour = birthday.hour
        lunar_day = lunar.getDayBySolar(birthday.year, birthday.month, birthday.day)

    shizhu = lunar.getShiGz(lunar_day.Lday2.tg, hour)  # 时柱

    gans = [g[0] for g in common.GANS]
    zhis = [z[0] for z in common.ZHIS]
    sizhu = [  # 四柱
        gans[lunar_day.Lyear2.tg] + zhis[lunar_day.Lyear2.dz],
        gans[lunar_day.Lmonth2.tg] + zhis[lunar_day.Lmonth2.dz],
        gans[lunar_day.Lday2.tg] + zhis[lunar_day.Lday2.dz],
        gans[shizhu.tg] + zhis[shizhu.dz],
    ]
    return MingPan(sizhu, gender, birthday)


def bazi_by_sizhu(sizhu, gender):
    """
    只有四柱，一般看古人的八字！
    既然是古人，就没有大运流年这一概念！！！
    """
    return MingPan(sizhu, gender)
